package com.quanyue.haircut.shop.service.impl;

import com.quanyue.auto.util.AssertUtils;
import com.quanyue.auto.util.KeyGenerator;
import com.quanyue.haircut.domain.entity.Card;
import com.quanyue.haircut.domain.entity.CardScope;
import com.quanyue.haircut.domain.entity.User;
import com.quanyue.haircut.shop.mapper.CardMapper;
import com.quanyue.haircut.shop.mapper.CardScopeMapper;
import com.quanyue.haircut.shop.service.CardService;
import com.vhuomai.mybatis.mapper.page.Page;
import com.vhuomai.mybatis.mapper.page.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CardServiceImpl implements CardService {
    @Autowired
    private CardMapper cardMapper;
    @Autowired
    private CardScopeMapper cardScopeMapper;

    @Override
    public Page <User> findUserByShop(PageRequest request, Integer shopId) {
        List<Card> cardList = cardMapper.findAll();
        return cardMapper.findUserByShop(request, shopId);
    }

    @Override
    public List <Card> findCardByUser(Integer userId) {
        return cardMapper.findByUserValid(userId);
    }

    @Override
    public Card createCard(Card card) {
        Card check = cardMapper.findByUserAndShopValid(card.getUserId(), card.getShopId());
        AssertUtils.isNull(check, "在该店铺已是会员");
        card.setCardNo(KeyGenerator.createOrderNo("XF", card.getMerchantNo(), "QY"));
        card.setDelFlag(false);
        card.setCreateTime(LocalDateTime.now());
        card.setModifyTime(LocalDateTime.now());
        cardMapper.insert(card);
        return card;
    }

    @Override
    public void updateCard(Card card) {
        Card check = cardMapper.findOne(card.getId());
        AssertUtils.notNull(check, "卡片信息校检出错");
        card.setModifyTime(LocalDateTime.now());
        card.setCreateTime(check.getCreateTime());
        card.setDelFlag(false);
        cardMapper.updateSelective(card);
    }

    @Override
    public void deleteCard(Integer cardId) {
        Card check = cardMapper.findOne(cardId);
        AssertUtils.notNull(check, "卡片信息校检出错");
        check.setDelFlag(true);
        check.setModifyTime(LocalDateTime.now());
        cardMapper.updateSelective(check);
    }

    @Override
    public void updateCardScope(Boolean isCommon, Integer merchantNo) {
        CardScope scope = this.selectCardScope(merchantNo);
        if (scope != null) {
            scope.setUpdateTime(LocalDateTime.now());
            scope.setIsCommon(isCommon);
            cardScopeMapper.update(scope);
        } else {
            scope = new CardScope();
            scope.setIsCommon(isCommon);
            scope.setCreateTime(LocalDateTime.now());
            scope.setUpdateTime(LocalDateTime.now());
            scope.setMerchantNo(merchantNo);
            cardScopeMapper.insert(scope);
        }
    }

    @Override
    public CardScope selectCardScope(Integer merchantNo) {
        return cardScopeMapper.findByMerchantNo(merchantNo);
    }

    @Override
    public Card findCardByUserAndShop(Integer userId, Integer shopId) {
        return cardMapper.findByUserAndShopValid(userId, shopId);
    }
}
